﻿Imports System.Web.UI
Imports System.Web


Namespace AboutDNN.DNNMVC

    Public Class BaseView
        Inherits UserControl

        Public Class Html
            Inherits HtmlHelper
        End Class

        Public Function Ajax(ByVal url As String) As AjaxRequest
            Return AjaxRequest.Create(url)
        End Function

        Public Function CreatePartialForm(ByVal formId As String) As String
            Return JSHelper.ValidateGroupScript(formId)
        End Function

        Public Function SubmitPartialForm(ByVal formId As String, ByVal url As String) As AjaxRequest
            Return SubmitPartialForm(formId, url, True)
        End Function

        Public Function SubmitPartialForm(ByVal formId As String, ByVal url As String, ByVal validate As Boolean) As AjaxRequest
            Dim ajax = AjaxRequest.Create(url)
            ajax.Post().PartialForm(formId)
            If validate Then ajax.Validate()
            Return ajax
        End Function

        Public Class JS
            Inherits JSHelper
        End Class

        Public Function RenderIf(ByVal condition As Boolean, ByVal text As String) As HtmlHelper.RenderIfBuilder
            Return Html.RenderIf(condition, text)
        End Function

        Public Function TB(ByVal tagName As String) As HtmlHelper.TagBuilder
            Return New HtmlHelper.TagBuilder(tagName)
        End Function

        ' THIS FIELD IS SET BY THE ROUTE.RENDERPARTIAL TO INDICATE TO THE BASE VIEW THAT IT IS BEING RENDERED BY IT.
        Public IsRenderPartial As Boolean = False

        Public Function RenderPartial(ByVal ViewName As String) As String
            Try
                Return RouteHelper.RenderPartial(App.MVCContext.Route.Controller, ViewName, Nothing)
            Catch ex As Exception
                App.HandleException(ex, False)
                Return RenderError(ex)
            End Try
        End Function

        Public Function RenderPartial(ByVal ViewName As String, ByVal data As Object) As String
            Try
                Return RouteHelper.RenderPartial(App.MVCContext.Route.Controller, ViewName, data)
            Catch ex As Exception
                App.HandleException(ex, False)
                Return RenderError(ex)
            End Try
        End Function

        Public Function RenderPartial(ByVal ControllerName As String, ByVal ViewName As String, ByVal data As Object) As String
            Try
                Return RouteHelper.RenderPartial(ControllerName, ViewName, data)
            Catch ex As Exception
                App.HandleException(ex, False)
                Return RenderError(ex)
            End Try
        End Function

        Public Function RenderPartialIf(ByVal Condition As Boolean, ByVal ViewName As String, ByVal Data As Object) As String
            If Condition Then
                Return RenderPartial(App.MVCContext.Route.Controller, ViewName, Data)
            Else
                Return ""
            End If
        End Function

        Public Function RenderPartialIf(ByVal Condition As Boolean, ByVal ControllerName As String, ByVal ViewName As String, ByVal Data As Object) As String
            If Condition Then
                Return RenderPartial(ControllerName, ViewName, Data)
            Else
                Return ""
            End If
        End Function

        Private Function RenderError(ByVal ex As Exception) As String
            Return RouteHelper.RenderPartial("shared", "error", ErrorViewModel.Create(ex))
        End Function

        Public Function E(ByVal data As String) As String
            Return HttpUtility.HtmlEncode(data)
        End Function

        Public Sub HandleViewError(ByVal ex As Exception)
            ' If it is not rendering in partial mode, then render the error.
            If IsRenderPartial = False Then
                App.HandleException(ex, False)
                App.MVCContext.Response.Write(RenderPartial("shared", "error", ErrorViewModel.Create(ex)))
            Else
                Throw ex
            End If
        End Sub


    End Class

End Namespace